<?php
/**
 * gpa_new Description
 * @package
 * @name controller_gallery
 * @filesource controller_gallery.php
 * @author Meta Sanjaya
 */

class controller_gallery extends Controller
{
    /**
     * @access public
     * @method __construct
     * @param void
     * @return void
     */
    public function  __construct()
    {
        parent::__construct();
        $model_menu = $this->getModel('menu');
        $this->_main_menus = createMenus($model_menu->getMenus());
        $this->_user_menus = createUserMenus();
    }

    public function view()
    {
        if(!User::getUserId()) {
            redirect(getAppUrl('user', 'login'), false);
        }

        $view = "gallery_html";

        $model = $this->getModel();

        $var['page_title']="Gallery";

        $var['row'] = getInstance('config')->get("gallery_list_row");
        $var['column'] = getInstance('config')->get("gallery_list_column");
        $var['images'] = $model->getImages("gallery", 0, $var['row']*$var['column']);
        $var['mainmenus'] = $this->_main_menus;
        $var['usermenus'] = $this->_user_menus;

        $this->addVars($var);
        $this->renderView($view);
    }

    public function comment()
    {        
        if(count(getInstance('request')->post) && !!User::getUserId()) {
            $model = $this->getModel();
            if($model->setImageComment(getInstance('request')->get['iid'], User::getUserId(), getInstance('request')->post['comment-field'])) {
                echo "comment added!";
            } else {
                echo "failed!";
            }
        } else {
            echo "failed!";
        }
    }

    public function view_image($param)
    {
        if(!User::getUserId()) {
            redirect(getAppUrl('user', 'login'), false);
        }
        
        $view = "view_image_html";
        $iid = $param[0];
        
        $model = $this->getModel();
        $model->viewImage($iid);
        $model_album = $this->getModel('album');
        $model_comment = $this->getModel('comment');

        $option['thumb_size'] = getInstance('config')->get('gallery_thumb_size');
        $var['mainmenus'] = $this->_main_menus;
        $var['usermenus'] = $this->_user_menus;
        $var['detail'] = $model->getImageById($iid, $option);
        $var['user_image']=$model_album->isUserImage($iid);
        $var['page_title']=$var['detail']['title'];

        //var_dump($var);
        //exit;

        $this->addVars($var);
        $this->renderView($view);
    }

    public function __default()
    {        
        redirect('gallery/view');
    }
}
?>